package se.rende.myrss;

import se.rende.myrss.data.Constants;
import se.rende.myrss.data.MyRSSDataHelper;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class AddChannelActivity extends Activity implements
		LoaderManager.LoaderCallbacks<Cursor> {

	public static final String ARG_CHANNEL_ID = "channel_id";
	private EditText urlField;
	private EditText titleField;
	private String channelId = null;
	private boolean isEdit = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_channel);
		titleField = (EditText) findViewById(R.id.add_channel_title);
		urlField = (EditText) findViewById(R.id.add_channel_url);
		
		Uri intentData = getIntent().getData();
		if (intentData != null) {
			// new channel with pre-filled fields
			urlField.setText(intentData.toString());
			titleField.requestFocus();
		} else {
			Bundle extras = getIntent().getExtras();
			if (extras == null) {
				// new channel with empty fields
			} else {
				channelId = extras.getString(ARG_CHANNEL_ID);
				if (channelId != null) {
					isEdit = true;
					Log.d("AddChannelActivity", "channeId=" + channelId);
					setTitle(R.string.title_activity_edit_channel);
					getLoaderManager().initLoader(0, null, this);
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_add_channel, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.add_channel_done:
			if (isEdit) {
				updateChannel();
			} else {
				createChannel();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void updateChannel() {
		MyRSSDataHelper dataHelper = new MyRSSDataHelper(this);
		SQLiteDatabase database = dataHelper.getWritableDatabase();
		database.execSQL(
				"update channel set name=?, url=?",
				new String[] { titleField.getText().toString(),
						urlField.getText().toString() });
		database.close();
		finish();
	}

	private void createChannel() {
		MyRSSDataHelper dataHelper = new MyRSSDataHelper(this);
		SQLiteDatabase database = dataHelper.getWritableDatabase();
		database.execSQL(
				"insert into channel (name, url, is_group) values (?, ?, 0)",
				new String[] { titleField.getText().toString(),
						urlField.getText().toString() });
		database.close();
		finish();
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			String[] projection = { "name", "url" };
			return new CursorLoader(this, Constants.CONTENT_URI_CHANNEL,
					projection, "_id=?",
					new String[] { channelId }, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				String name = cursor.getString(0);
				String url = cursor.getString(1);
				
				titleField.setText(name);
				urlField.setText(url);
			}
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		
	}

}
